//
//  ClubOpenServerListViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/13.
//

import UIKit
import JXSegmentedView
import Cosmos

class ClubOpenServerListViewController: ClubBaseViewController {

    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(12.0.Scale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .font(16.font_semiBold).color(.white).end()
    private let subLabel = c_make_ex(UILabel.self)
        .font(12.font_medium).color("#EDEEF0").end()
    private let logoLabel =  c_make_ex(UILabel.self)
        .color(.white).font(11.font_wide).text("乐玩TOP")
        .alignment(.right).end()
    private let scoreLabel =  c_make_ex(UILabel.self)
        .color(.white).font(24.font_wide).alignment(.right).end()
    private var starView = CosmosView()
    private var emptyView = ClubServerEmptyView()
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubOpenServerCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 82.0.Scale
        return tableView
    }()
    
    var topModel: ClubGameDetailModel? {
        didSet{
            guard let md = topModel else { return }
            
            self.iconImageView.loadImage(url: md.icon)
            self.nameLabel.text = md.name
            self.scoreLabel.text = "\(md.point)"
            self.starView.rating = md.point/2
            self.subLabel.text = md.type
        }
    }
    var gamesID: String = "" {
        didSet{
            self.loadData()
        }
    }
    
    private var sourceData = [ClubOpenServiceModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        
        view.subviews{
            iconImageView
            nameLabel
            subLabel
            logoLabel
            scoreLabel
            starView
            tableView
            emptyView
        }
        
        iconImageView.leading(12.0.Scale).top(8.0.Scale).size(60.0.Scale)
        nameLabel.leading(80.0.Scale).top(10.0.Scale).trailing(75.0.Scale)
        subLabel.leading(80.0.Scale).top(30.0.Scale).trailing(75.0.Scale)
        logoLabel.trailing(22.0.Scale).top(18.0.Scale)
        scoreLabel.trailing(22.0.Scale).top(25.0.Scale)
        starView.leading(80.0.Scale)
        starView.Bottom == iconImageView.Bottom
        tableView.leading(0).trailing(0).bottom(.safe_bottom)
        tableView.Top == iconImageView.Bottom + 8.0.Scale
        emptyView.leading(0).trailing(0).bottom(0)
        emptyView.Top == iconImageView.Bottom + 8.0.Scale
        
        starView.rating = 0
        starView.settings.starSize = 12.0.Scale
        starView.settings.starMargin = 5
        starView.settings.fillMode = .half
        starView.settings.emptyImage = "Common_emptyStar".toImage
        starView.settings.filledImage = "Common_fillStar".toImage
        
        emptyView.isHidden = true
    }

    private func loadData(){
        
        Task{
            let res = await network.requestSpecialData(.openService(gameId: gamesID), type: ClubOpenServiceModel.self, isShowError: false)
            if res.isSuccess, let models = res.models {
                
                emptyView.isHidden = models.count > 0
                self.sourceData = models
                self.tableView.reloadData()
            }
        }
    }
}

extension ClubOpenServerListViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return sourceData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubOpenServerCell", for: indexPath) as! ClubOpenServerCell
        cell.model = sourceData[indexPath.row]
        return cell
    }

}

//MARK: 开服cell
class ClubOpenServerCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor("#F7F8FA").corner(12.0.Scale).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(14.font_medium).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#FF7700").font(14.font_semiBold).end()
    private let stateLabel = c_make_ex(UIButton.self)
        .color("#7D7E80").color("#FF7700", state: .selected)
        .font(14.font_semiBold).bgImage(UIImage.colorToImage(color: "#E1E2E5".toColor))
        .bgImage(UIImage.colorToImage(color: .clear), state: .selected).corner(4)
        .end()
    
    var model: ClubOpenServiceModel?{
        didSet{
            guard let md = model else { return }
            
            timeLabel.text = md.time1 + " " + md.time2
            nameLabel.text = md.server_name
            stateLabel.setTitle(md.type, for: .normal)
            if md.type == "已开服" {
                stateLabel.isSelected = true
                stateLabel.layer.borderWidth = 1
                stateLabel.layer.borderColor = "#FFC089".toColor.cgColor
            }else{
                stateLabel.isSelected = false
                stateLabel.layer.borderWidth = 1
                stateLabel.layer.borderColor = UIColor.clear.cgColor
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                timeLabel
                nameLabel
                stateLabel
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(8.0.Scale).bottom(8.0.Scale).height(66.0.Scale)
        nameLabel.leading(12.0.Scale).top(16.0.Scale).width(<=160.0.Scale)
        timeLabel.leading(12.0.Scale).top(36.0.Scale).width(<=160.0.Scale)
        stateLabel.trailing(12.0.Scale).width(60.0.Scale).height(22.0.Scale).centerVertically()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ClubOpenServerListViewController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}



class ClubServerEmptyView: UIView {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Common_openServiceEmpty").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).alignment(.center)
        .text("暂无开服消息哦~").end()
    
    var titleString: String = "" {
        didSet{
            titleLabel.text = titleString
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            iconImageView
            titleLabel
        }
        
        iconImageView.top(120.0.Scale).width(160.0.Scale).height(152.0.Scale).centerHorizontally()
        titleLabel.leading(20).trailing(20)
        titleLabel.Top == iconImageView.Bottom + 16.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
